//切换密码登录和验证码登录
var changelogin = document.querySelector("#message")
var mima_form = document.querySelector(".mima-form")
var logging = document.querySelector(".logging")
var code_form = document.querySelector(".code-form")
var error_tip = document.querySelector(".error-tip")
changelogin.onclick = function () {
    if (code_form.style.display == "none") {
        logging.innerText = "短信登录"
        changelogin.innerText = "账号登录"
        mima_form.style.display = "none"
        code_form.style.display = "block"
        error_tip.style.display = "none"
    }else {
        logging.innerText = "账号登录"
        changelogin.innerText = "短信登录"
        mima_form.style.display = "block"
        code_form.style.display = "none"
        error_tip.style.display = "none"
    }
}

//验证手机号
$(".login-name").blur(function () {
    var username =$(".login-name").val();
    var ele
    var error_tip = $(".error-tip")
    error_tip.html("")
    if(username.trim() ==null || username.trim() ==""){
        ele ="<b></b>\n" +
            "                <p>请输入手机号</p>"
        error_tip.html(ele)
        error_tip.css("display","block")
        return false;
    }else if(!/^[1][3,4,5,7,8][0-9]{9}$/.test(username)){
        ele ="<b></b>\n" +
            "                <p>请输入正确的手机号</p>"
        error_tip.html(ele)
        error_tip.css("display","block")
        return false;
    }else{
        error_tip.html("")
        error_tip.css("display","none")
        return true;
    }
})
//验证短信登录手机号
$(".code-username").blur(function () {
    var username =$(".code-username").val();
    var ele
    var error_tip = $(".error-tip")
    error_tip.html("")
    if(username.trim() ==null || username.trim() ==""){
        ele ="<b></b>\n" +
            "                <p>请输入手机号</p>"
        error_tip.html(ele)
        error_tip.css("display","block")
        return false;
    }else if(!/^[1][3,4,5,7,8][0-9]{9}$/.test(username)){
        ele ="<b></b>\n" +
            "                <p>请输入正确的手机号</p>"
        error_tip.html(ele)
        error_tip.css("display","block")
        return false;
    }else{
        error_tip.html("")
        error_tip.css("display","none")
        return true;
    }
})

//验证密码登录密码
$(".login-password").blur(function () {
    var usepwd =$(".login-password").val()
    var error_tip = $(".error-tip")
    var ele
    error_tip.html("")
    if(usepwd.trim() ==null || usepwd.trim() ==""){
        ele ="<b></b>\n" +
            "                <p>请输入密码</p>"
        error_tip.html(ele)
        error_tip.css("display","block")

    }else if(!/^[0-9A-Za-z]{6,12}$/.test(usepwd)){
        ele ="<b></b>\n" +
            "                <p>请输入正确密码</p>"
        error_tip.html(ele)
        error_tip.css("display","block")

    }else{
        error_tip.html("")
        error_tip.css("display","none")
    }
})

//点击账号密码登录
$(".login-to").click(function () {
    var username = $(".login-name").val();
    var usepwd = $(".login-password").val()
    var error_tip = $(".error-tip")
    var ele
    error_tip.html("")
    if ((username.trim() ==null || username.trim() =="")&&(usepwd.trim() ==null || usepwd.trim() =="")){
        ele ="<b></b>\n" +
            "                <p>请输入手机号和密码</p>"
        error_tip.html(ele)
        error_tip.css("display","block")
    } else {
        accountlogin()
    }
})

//==========后端检查========================
//***************账号密码登录***************
function accountlogin() {
    var custLoginAccount = $(".login-name").val();
    console.log(custLoginAccount);
    var custPassword =$(".login-password").val()
    console.log(custPassword);
    //正则表达式 0:手机 ， 1 邮箱 2 用户名
    var formdata = "&Telno="+custLoginAccount+"&custPassword="+custPassword
    console.log(formdata);
    $.ajax({
        type:"post",
        url:"/fresh/customer/login",
        data:formdata,
        datatype:"json",
        success:function (result) {
            console.log(result);
            if (result.status == 0){//登录失败，用户与或密码错误
                $(".loginMsg").text(result.data);
                console.log(result.data);
                console.log("登陆失败。。。。。");
            }else {//登录成功，跳转到首页
                $(".loginMsg").text("");
                console.log("登录成功");
                window.location.href="/fresh/index.jsp";
            }
        }
    })
}

//******************验证码登录*********************

//获取验证码
var interValObj ;// 计时器变量，控制时间
var count = 60; // 间隔函数
var curCount ;//当前剩余秒数

//2. 获得按钮点击，点击后，按钮内部显示 倒计时
$(".code-btn").click(function () {
    curCount = count
    $(".code-btn").attr("disabled","true")
    $(".code-btn").val(curCount+"秒后重新发送")
    $(".code-btn").css("font-size","12px")
    interValObj = window.setInterval(setRemainTime,1000)
})

//计时器
function setRemainTime() {
    if (curCount == 0){
        window.clearInterval(interValObj)
        $(".code-btn").removeAttr("disabled")
        $(".code-btn").val("重新发送验证码")
    }else {
        curCount--
        $(".code-btn").val(curCount + "秒后重新发送");
    }
}

//验证验证码
var serverCode ;//服务器中的而短信验证码
//  获得页面手机号，ajax请求到后端先检查手机号，若没被注册过，获得验证码，服务器返回验证码
$(".code-btn").click(function () {
    var telno =$(".code-username").val();// 获得手机号
    $.ajax({
        type:"post",
        url:"/fresh/customer/codeL",
        data:"telno="+telno,
        datatype:"json",
        success:function (result) {
            console.log(result)
            //??????????
            var error_tip = $(".error-tip")
            if(result.status ==2){//成功调用
                //用户输入验证码
                serverCode = result.data;//后端返回的验证码
                console.log(serverCode);
                error_tip.html("")
                error_tip.css("display","none")

            }else{//调用失败
                var tip = result.data;
                var ele ="<b></b>\n" +
                    "                <p>"+tip+"</p>"
                error_tip.html(ele)
                error_tip.css("display","block")
                window.clearInterval(interValObj);
                $(".code-btn").removeAttr("disabled");
                $(".code-btn").val("重新发送验证码");
            }
        }
    })
})

$(".code-login-to").click(function () {
    var telno =$(".code-username").val();// 获得手机号
    var error_tip = $(".error-tip")
    var  ele
    if(telno.trim() ==null || telno.trim() ==""){
       ele ="<b></b>\n" +
            "                <p>请输入手机号</p>"
        error_tip.html(ele)
        error_tip.css("display","block")
    }else {
        var inputCode = $(".code-inpt").val()
        if (inputCode == serverCode) {
            error_tip.html(ele)
            error_tip.css("display","none")
            codeLogin(telno)

        }else {
            //用户输入的验证码错误
            ele ="<b></b>\n" +
                "                <p>用户输入验证码错误</p>"
            error_tip.html(ele)
            error_tip.css("display","block")
            console.log("登陆失败。。。。。");
        }
    }
})

function codeLogin(telno) {
    $.ajax({
        type:"post",
        url:"/fresh/customer/codeLogin",
        data:"Telno="+telno,
        datatype:"json",
        success:function (result) {
            if (result.status==1){
                console.log("登陆成功");
                window.location.href="/fresh/index.jsp";
            } else {
                console.log("登陆失败");
            }
        }
    })
}






